﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using DevComponents.AdvTree;
using System.Collections;
using System.Collections.Specialized;
using System.Security.AccessControl;


using IconH;
using CopyOfTeacher;
//Hàm lấy properties của hệ thống
using Haiproperties;
//Dùng thư viện lấy hàm của hệ thống
using Microsoft.VisualBasic.FileIO;

namespace Winexplore_V2
{
    public partial class Form1 : DevComponents.DotNetBar.Office2007RibbonForm
    {
        DirectoryInfo curDir;
        bool s_hidden = false;
        bool backNext = false;
        int typeClipBorad;
        //= 1: copy
        //= 2; cut;

        //ArrayList clipBoard = new ArrayList();
        static List<DirectoryInfo> clipBoardD = new List<DirectoryInfo>();
        static List<FileInfo> clipBoardF = new List<FileInfo>();

        //AdvTree history = new AdvTree(); 
        Node history = new Node();

        public Form1()
        {
            InitializeComponent();

            //Dinh vị trí mới cho form
            this.Location = new Point(250, 10);
            this.ClientSize = new Size(800, 600);

            //Tra ve chi so cua phan tu moi add
            int i = history.Nodes.Add(new Node(""));
            history = history.Nodes[i];
            cb_showHidden.Checked = s_hidden;

            //Load drive
            tvwDir.Nodes.Clear();
            CreateTree();

            btn_Paste.Enabled = false;
            btn_Previous.Enabled = false;
            btn_Next.Enabled = false;
            btn_conTextPaste.Enabled = false;
        }

        private void CreateTree()
        {
            //Add MyDocument node
            Node myDocument = new Node("My Documents");
            myDocument.Tag = new DirectoryInfo( SpecialDirectories.MyDocuments);
            myDocument.Image = resPicture.myDocument;
            loadNodeCon(myDocument);
            tvwDir.Nodes.Add(myDocument);

            //Add MyPictures node
            Node myPictures = new Node("My Pictures");
            myPictures.Tag = new DirectoryInfo(SpecialDirectories.MyPictures);
            myPictures.Image = resPicture.myPicture;
            loadNodeCon(myPictures);
            tvwDir.Nodes.Add(myPictures);

            //Add MyVideos node
            Node myMusics = new Node("My Musics");
            myMusics.Tag = new DirectoryInfo(SpecialDirectories.MyMusic);
            myMusics.Image = resPicture.myMusic;
            loadNodeCon(myMusics);
            tvwDir.Nodes.Add(myMusics);

            //Add desktop node
            Node desktop = new Node("Desktop");
            desktop.Tag = new DirectoryInfo(SpecialDirectories.Desktop);
            desktop.Image = resPicture.myDesktop;
            loadNodeCon(desktop);
            tvwDir.Nodes.Add(desktop);

            //Add computer node
            Node myComputerNode = new Node("My Computer");
            myComputerNode.Name = "My Computer";
            myComputerNode.Tag = "mycomputer" ;
            myComputerNode.Image = resPicture.myComputer;
            tvwDir.Nodes.Add(myComputerNode);

            //Add các drive
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                string name = string.Format(
                    "{0} ({1})",
                    drive.IsReady ? drive.VolumeLabel : drive.DriveType.ToString(),
                    drive.Name
                );
                Node driveNode = new Node(name);
                //driveNode.Name = drive.RootDirectory.FullName;
                driveNode.Name = name;
                driveNode.Tag = drive.RootDirectory;
                driveNode.Image = resPicture.disk ;
                myComputerNode.Nodes.Add(driveNode);
                btn_Previous.Enabled = true;
                loadNodeCon(driveNode);
            }
            
            tvwDir.SelectedNode = myComputerNode;
            //Lúc nhấn mở rộng một node bên tree
            tvwDir.SelectedNode.Expand();
            
        }

        private void loadNodeCon(Node node)
        {
            // nếu node hiện tại đang được chọn là My computer thì dừng 
            if (node.Tag.ToString() == "mycomputer")
            {
                return;
            }

            //Ép kiểu về directory
            DirectoryInfo dir = (DirectoryInfo)node.Tag;
            DirectoryInfo[] subDirs = null;

            try
            {
                subDirs = dir.GetDirectories();
            }
            catch (Exception)
            {
                return;
            }

            node.Nodes.Clear();
            foreach (DirectoryInfo subDir in subDirs)
            {
                //nếu có chứa thuộc tính hidden trong đó
                bool hasHidden = subDir.Attributes.ToString().Contains("Hidden");
                if(hasHidden == true && s_hidden == false)
                    continue;
                Node nodes = new Node(subDir.Name);
                nodes.Name = subDir.FullName;
                nodes.Tag = subDir;
                nodes.Image = resPicture.folder;
                node.Nodes.Add(nodes);
            }
        }

        private void tvwDir_Click(object sender, EventArgs e)
        {
            if (tvwDir.SelectedNode.Tag.GetType() == typeof(DirectoryInfo))
            {
                curDir = (DirectoryInfo)tvwDir.SelectedNode.Tag;
                OpenDirectory();
            }
        }
        
        private void OpenDirectory()
        {
            if (curDir == null)
                return;
            txt_address.Text = curDir.FullName;
            lvwDir.Items.Clear();

            DirectoryInfo[] subDirs = null;
            try
            {
                subDirs = curDir.GetDirectories();
            }
            catch { }

            if (subDirs == null)
            {
                MessageBox.Show("Access Denied", "Explorer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            foreach (DirectoryInfo subDir in subDirs)
            {
                //Kiểm tra file ẩn
                bool hidden = subDir.Attributes.ToString().Contains("Hidden");

                if ((hidden == true) && (s_hidden == false))
                    continue;
                
                ListViewItem lvi = lvwDir.Items.Add(subDir.Name);
                lvi.Tag = subDir;
                lvi.Name = subDir.FullName;
                lvi.BackColor = Color.Transparent;
                lvi.ImageIndex = 2;
                lvi.SubItems.Add("Folder");
                
            }
            foreach (FileInfo file in curDir.GetFiles())
            {
                bool hiden = file.Attributes.ToString().Contains("Hidden");
                if (hiden && s_hidden == false)
                    continue;

                ListViewItem lvi = lvwDir.Items.Add(file.Name);
                lvi.Tag = file;
                lvi.BackColor = Color.Transparent;

                if (file.Extension != ".exe")
                {
                    //Lấy icon của hệ thống qua hàm iconhelper
                    if(imageLarge.Images.IndexOfKey(file.Extension) == -1)
                    {
                        Icon ic = IconHelper.GetFileIcon(file.FullName, IconSize.Large);
                        imageLarge.Images.Add(file.Extension, ic.ToBitmap());
                        ic = IconHelper.GetFileIcon(file.FullName, IconSize.Small);
                        imageSmall.Images.Add(file.Extension, ic.ToBitmap());
                    }
                    lvi.ImageIndex = lvi.ImageList.Images.IndexOfKey(file.Extension);
                }
                else
                {
                    if (imageLarge.Images.IndexOfKey(file.Extension) == -1)
                    {
                        Icon ic = IconHelper.GetFileIcon(file.FullName, IconSize.Large);
                        imageLarge.Images.Add(file.FullName, ic.ToBitmap());
                        ic = IconHelper.GetFileIcon(file.FullName, IconSize.Small);
                        imageSmall.Images.Add(file.FullName , ic.ToBitmap());
                    }
                    lvi.ImageIndex = lvi.ImageList.Images.IndexOfKey(file.FullName );
                }

                lvi.SubItems.Add(file.LastWriteTime.ToString());
                lvi.SubItems.Add("File folder");
                lvi.SubItems.Add((file.Length / 1024).ToString() + " KB");
            }
            lb_status.Text = string.Format("Total {0} items", lvwDir.Items.Count);
                
        }

#region Code View Selection

        private void btn_LargeIcon_Click_1(object sender, EventArgs e)
        {
            btn_view.Icon = btn_LargeIcon.Icon;
            btn_view.Text = btn_LargeIcon.Text;
            lvwDir.View = View.LargeIcon;
        }


        private void btn_SmallIcon_Click(object sender, EventArgs e)
        {
            btn_view.Icon = btn_SmallIcon.Icon;
            btn_view.Text = btn_SmallIcon.Text;
            lvwDir.View = View.SmallIcon;
        }

        private void btn_List_Click(object sender, EventArgs e)
        {
            btn_view.Icon = btn_List.Icon;
            btn_view.Text = btn_List.Text;
            lvwDir.View = View.List;
        }

        private void btn_Details_Click(object sender, EventArgs e)
        {
            btn_view.Icon = btn_Details.Icon;
            btn_view.Text = btn_Details.Text;
            lvwDir.View = View.Details;
        }

        private void btn_Titles_Click(object sender, EventArgs e)
        {
            btn_view.Icon = btn_Titles.Icon;
            btn_view.Text = btn_Titles.Text;
            lvwDir.TileSize = new Size(250, 40);
            lvwDir.View = View.Tile;
        }

        private void btn_view_Click(object sender, EventArgs e)
        {
            switch (lvwDir.View)
            {
                case View.LargeIcon: btn_Details.RaiseClick(); break;
                case View.SmallIcon: btn_LargeIcon.RaiseClick(); break;
                case View.List: btn_List.RaiseClick(); break;
                case View.Details: btn_SmallIcon.RaiseClick(); break;
                case View.Tile: btn_Titles.RaiseClick(); break;
                default: btn_Details.RaiseClick(); break;
            }
        }

#endregion

        private void tvwDir_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            //Lay nodes dang duoc chon
            try
            {
                Node selectedNode = e.Node;
                if (!backNext)  //nếu ng dùng nhấn nút back .. xóa hết trong history tại node đó. sau đó add dzo lại
                {
                    history.Nodes.Clear();                  
                    int i = history.Nodes.Add(new Node(selectedNode.Name));
                    history = history.Nodes[i];
                }
                backNext = false;
                //Nếu node đang được chọn là computer
                if (selectedNode.Tag.ToString() == "mycomputer")
                {
                    //Load qua listview
                    lvwDir.Items.Clear();
                    foreach (Node n in e.Node.Nodes)
                    {
                        ListViewItem li = new ListViewItem(n.Name);
                        li.Tag = n.Tag;
                        li.Name = n.Name;
                        li.BackColor = Color.Transparent;
                        //Kiểm tra file ảnh có trong iamgelist chưa?
                        //if (imageLarge.Images.IndexOfKey("disk") == -1)
                        //{
                        //    imageLarge.Images.Add("disk", n.Image);
                        //    imageSmall.Images.Add("disk", n.Image);
                        //    n.ImageIndex = imageSmall.Images.IndexOfKey("disk");
                        //}
                        li.ImageIndex = 1;
                        lvwDir.Items.Add(li);
                    }
                    btnUp.Enabled = false;
                }
                else if (selectedNode.Tag is DirectoryInfo)
                {
                    curDir = (DirectoryInfo)selectedNode.Tag;
                    btn_Previous.Enabled = true;
                    btnUp.Enabled = true;
                    OpenDirectory();
                }
                //kiểm tra tag có phải là kiểu Directory Info.

                if (selectedNode.Tag.GetType() == typeof(DirectoryInfo))
                {
                    //Xoa danh sach node da co
                    selectedNode.Nodes.Clear();
                    //Them cac node thu muc con
                    DirectoryInfo dir = (DirectoryInfo)selectedNode.Tag;
                    foreach (DirectoryInfo subDir in dir.GetDirectories())
                    {
                        Node dirNode = new Node(subDir.Name);
                        dirNode.Tag = subDir;
                        dirNode.Name = subDir.FullName;
                        dirNode.Image = resPicture.folder;
                        selectedNode.Nodes.Add(dirNode);
                    }

                    //hien thi qua ben listview
                    curDir = dir;
                    OpenDirectory();
                }
                else if (selectedNode.Tag.ToString() == "Mycomputer")
                {
                    lvwDir.Items.Clear();
                    foreach (DriveInfo drv in DriveInfo.GetDrives())
                    {
                        ListViewItem li = new ListViewItem();
                        if (drv.IsReady)
                        {
                            li.Text = string.Format("{0} ({1})", drv.VolumeLabel, drv.Name);
                            li.ImageIndex = 2;
                            li.Tag = drv;
                        }
                        else
                        {
                            li.Text = string.Format("{0} ({1})", drv.DriveType, drv.Name);
                            li.ImageIndex = 2;
                            li.Tag = drv.DriveType.ToString();
                        }
                        lvwDir.Items.Add(li);
                    }

                }
                //Mo rong node
                selectedNode.Expand();
            }
            catch (Exception)
            {
                return;
            }
            
        }


        private void loadNodeCap2(Node n)
        {
            try
            {
                foreach (Node node in n.Nodes)
                {
                    DirectoryInfo dr = (DirectoryInfo)node.Tag;
                    try
                    {
                        if (dr.GetDirectories().Length > 0)
                            node.Nodes.Add(new Node());
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Access Denied", "^^@ Beta Explorer @^^", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            
        }

        private void tvwDir_AfterExpand(object sender, AdvTreeNodeEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                loadNodeCon(e.Node);
                loadNodeCap2(e.Node);
            }
        }

        //khi kich chon vao list view
        private void lvwDir_ItemActivate(object sender, EventArgs e)
        {
            //chon thang dau tien trong nhung thang duoc chon
            ListViewItem li = lvwDir.SelectedItems[0];
            if (li.Tag is DirectoryInfo)
            {
                curDir = (DirectoryInfo)li.Tag;
                tvwDir.SelectedNode.Expand();
                tvwDir.SelectedNode = tvwDir.SelectedNode.Nodes.Find(li.Text, false )[0];
            }
            else
            {
                //Nếu là file có đuôi trực tiếp thì chạy luôn
                FileInfo file = (FileInfo)lvwDir.SelectedItems[0].Tag;
                System.Diagnostics.Process.Start(file.FullName);
            }
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            OpenDirectory();
            loadNodeCon(tvwDir.SelectedNode);
            loadNodeCap2(tvwDir.SelectedNode);
        }

        private void cb_showHidden_Click(object sender, EventArgs e)
        {
            s_hidden = cb_showHidden.Checked;
            btn_refresh_Click(sender, e);   
        }

        private void btn_Properties_Click(object sender, EventArgs e)
        {
            //nếu nó đang ở node computer
            if (curDir == null)
            {
                System.Diagnostics.Process.Start("control.exe", "System");
                return;
            }

            string file = curDir.FullName;
            if (lvwDir.SelectedItems.Count > 0)
            {
                ListViewItem li = lvwDir.SelectedItems[0];
                if (li.Tag.GetType() == typeof(FileInfo))
                {
                    FileInfo fi = (FileInfo)li.Tag;
                    file = fi.FullName;
                }
                else
                {
                    DirectoryInfo fi = (DirectoryInfo)li.Tag;
                    file = fi.FullName;
                }
            }
            FileFolder.ShowProperties(file);
        }

        private void lvwDir_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F2)
                {
                    {
                        lvwDir.SelectedItems[0].BeginEdit();
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
                
        }

        private void btn_Next_Click(object sender, EventArgs e)
        {
            //Kiểm tra nếu Node mà history đang nắm giữ có node con thì...
            if (history.HasChildNodes)
            {
                //enable button previous
                btn_Previous.Enabled = true;
                //Bật biến lên true tức là đang nhấn next
                backNext = true;
                //Chọn ra thằng chọn đầu tiên trong history
                history = history.Nodes[0];
                tvwDir.SelectedNode = tvwDir.Nodes.Find(history.Text, true)[0];
                if (history.HasChildNodes == false)
                    btn_Next.Enabled = false;
            }
        }

        private void btn_Previous_Click(object sender, EventArgs e)
        {
            //nếu cha nó khác null thì làm
            if (history.Parent != null)
            {
                backNext = true;
                btn_Next.Enabled = true;
                history = history.Parent;
                tvwDir.SelectedNode = tvwDir.Nodes.Find(history.Text, true)[0];
                if (history.Text == "My Computer")
                    btn_Previous.Enabled = false;
                
                btn_Next.Enabled = true;
            }
        }
        private void btn_undo_Click(object sender, EventArgs e)
        {
            btn_Previous.RaiseClick();

        }
        private void btnUp_Click(object sender, EventArgs e)
        {
            backNext = false;
            if (tvwDir.SelectedNode.Parent != null)
                tvwDir.SelectedNode = tvwDir.SelectedNode.Parent;
        }

        private void tvwDir_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back || (e.Alt && e.KeyCode == Keys.Left))
                btn_Previous.RaiseClick();
            if (e.Alt && e.KeyCode == Keys.Right)
                btn_Next.RaiseClick();
            if (e.Alt && e.KeyCode == Keys.Up)
                btnUp.RaiseClick();
            if (e.Control && e.KeyCode == Keys.A)
            {
                foreach (ListViewItem li in lvwDir.Items)
                    li.Selected = true;
            }
        }

        private void btn_Rename_Click(object sender, EventArgs e)
        {
            if (lvwDir.SelectedItems.Count > 0)
            {
                ListViewItem li = lvwDir.SelectedItems[0];
                if (li.Text != null)
                {
                    lvwDir.SelectedItems[0].BeginEdit();
                }
            }
        }

        private void clipBoard()
        {
            clipBoardF.Clear();
            clipBoardD.Clear();

            //anable button paste lên
            btn_Paste.Enabled = true;
            btn_conTextPaste.Enabled = true;
            //Kiểm tra xem nếu nó là loại directory thì cho lip tương ứng
            //fileinfo thì cho dzo clip tương ứng

            foreach (ListViewItem li in lvwDir.SelectedItems)
            {
                if (li.Tag is DirectoryInfo)
                {
                    clipBoardD.Add((DirectoryInfo)li.Tag);
                }
                else
                {
                    if (li.Tag is FileInfo)
                    {
                        clipBoardF.Add((FileInfo)li.Tag);
                    }
                }
            }
        }
        private void btn_Copy_Click(object sender, EventArgs e)
        {
            typeClipBorad = 1;
            clipBoard();
        }

        private static bool MoveDirectory(string src, string des)
        {
            bool ret;
            try
            {
                src = src.EndsWith(@"\") ? src : src + @"\";
                des = des.EndsWith(@"\") ? des : des + @"\";

                if (Directory.Exists(src))
                {
                    if (!Directory.Exists(des)) Directory.CreateDirectory(des);

                    foreach (string fls in Directory.GetFiles(src))
                    {
                        FileInfo flinfo = new FileInfo(fls);
                        flinfo.MoveTo(des + flinfo.Name);
                    }
                    foreach (string drs in Directory.GetDirectories(src))
                    {
                        DirectoryInfo drinfo = new DirectoryInfo(drs);
                        if (!MoveDirectory(drs, des + drinfo.Name))
                            ret = false;
                        Directory.Delete(drinfo.FullName);
                    }
                }
                ret = true;
            }
            catch (Exception)
            {
                ret = false;
            }
            return ret;
        }
        private void btn_Paste_Click(object sender, EventArgs e)
        {
            try
            {
                string des = curDir.FullName;
                if (typeClipBorad == 1) //Nếu nó là copy
                {
                    foreach (DirectoryInfo dr in clipBoardD)
                    {
                        if (des.Contains(dr.FullName))
                        {
                            MessageBox.Show("Bạn không thể sao chép thư mục hiện tại vào trong thư mục con của nó!");
                            return;
                        }

                        //Nối chuỗi và tạo thư mục mới
                        string desD = des + "\\" + dr.Name;
                        Directory.CreateDirectory(desD);
                        CopyDir.CopyDirectory(dr.FullName, desD, true);
                    }
                    foreach (FileInfo fi in clipBoardF)
                    {
                        try
                        {
                            FileInfo file = new FileInfo(fi.FullName);
                            file.CopyTo(des + "\\" + file.Name, true);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Tập tin này đã tồn tại!");
                        }
                    }
                }
                else
                {
                    if (typeClipBorad == 2) //Nếu nó là cut
                    {
                        if (clipBoardD != null)
                        {
                            foreach (DirectoryInfo dr in clipBoardD)
                            {
                                if (des.Contains(dr.FullName))
                                {
                                    MessageBox.Show("Bạn không thể sao chép thư mục hiện tại vào trong thư mục con của nó!");
                                    return;
                                }

                                string desD = des + "\\" + dr.Name;
                                if (desD != dr.FullName)
                                {
                                    Directory.CreateDirectory(desD);
                                    MoveDirectory(dr.FullName, desD);
                                    Directory.Delete(dr.FullName);
                                }
                            }
                        }
                    }
                    if (clipBoardF != null)
                    {
                        foreach (FileInfo fi in clipBoardF)
                        {
                            try
                            {
                                FileInfo file = new FileInfo(fi.FullName);
                                file.MoveTo(des + "\\" + file.Name);
                            }
                            catch (Exception)
                            {
                                MessageBox.Show("Tập tin này đã tồn tại");
                            }
                        }
                    }
                }
                btn_refresh.PerformClick();
                btn_Paste.Enabled = false;
                btn_conTextPaste.Enabled = false;
            }
            catch (Exception)
            {
                return;
            }
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            panelLeft.Visible = !panelLeft.Visible;
        }

        private void lvwDir_ItemActivate_1(object sender, EventArgs e)
        {
            btn_Paste.Enabled = true;
            ListViewItem li = lvwDir.SelectedItems[0];
            if (li.Tag is DirectoryInfo)
            {
                curDir = (DirectoryInfo)li.Tag;
                tvwDir.SelectedNode.Expand();
                tvwDir.SelectedNode = tvwDir.SelectedNode.Nodes.Find(li.Name, false)[0];
            }
            else
            {
                FileInfo fi = (FileInfo)lvwDir.SelectedItems[0].Tag;
                System.Diagnostics.Process.Start(fi.FullName);
            }
        }

        private void lvwDir_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            if (e.Label == null)
            {
                e.CancelEdit = true;
            }
            else
            {
                ListViewItem li = lvwDir.Items[e.Item];
                if (li.Tag is DirectoryInfo)
                {
                    DirectoryInfo d = li.Tag as DirectoryInfo;
                    string newPath = d.Parent.FullName + "\\" + e.Label;

                    if (Directory.Exists(newPath))
                    {
                        DialogResult rs = MessageBox.Show("Do you want to merge this folder with " + e.Label + "?", "Beta Explorer", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (rs == DialogResult.No)
                        {
                            e.CancelEdit = true;
                            return;
                        }
                    }
                    FileSystem.MoveDirectory(d.FullName, newPath, true);
                    li.Tag = new DirectoryInfo(newPath);
                    loadNodeCon(tvwDir.SelectedNode);
                }
                else if (li.Tag is FileInfo)
                {
                    FileInfo fi = li.Tag as FileInfo;
                    string fileName = fi.FullName;
                    string newPath = fileName.Substring(0, fileName.LastIndexOf('\\')) + "\\" + e.Label;
                    int i = 1;
                    if (File.Exists(newPath))
                    {
                        while (File.Exists(newPath))
                        {
                            newPath = newPath.Insert(newPath.LastIndexOf('.'), string.Format("({0})", i++));
                            fileName = newPath.Substring(newPath.LastIndexOf("\\"));
                        }
                        string message = string.Format("Do you want to rename \r\n\"{0}\"  to \r\n\"{1}\" ? \r\n\nThere is already a file with same name in this location", e.Label, fileName);
                        DialogResult rs = MessageBox.Show(message, "Beta Explorer", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (rs == DialogResult.No)
                        {
                            e.CancelEdit = true;
                            return;
                        }
                    }
                    fi.MoveTo(newPath);
                    li.Tag = new FileInfo(newPath);
                }
            }
        }

        private void lvwDir_Click(object sender, EventArgs e)
        {
            btn_Previous.Enabled = true;
        }

        private void tvwDir_BeforeExpand(object sender, AdvTreeNodeCancelEventArgs e)
        {
            if (e.Node.Tag != null)
                loadNodeCap2(e.Node);
        }

        private void lvwDir_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (lvwDir.Items.Count > 1)
            {
                lb_status.Text = string.Format("Total {0} items selected", lvwDir.Items.Count);
            }
        }

        private void lvwDir_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back || (e.Alt && e.KeyCode == Keys.Left))
                btn_Previous.RaiseClick();
            if (e.Alt && e.KeyCode == Keys.Right)
                btn_Previous.RaiseClick();
            if (e.Alt && e.KeyCode == Keys.Up)
                btnUp.RaiseClick();
            if (e.Control && e.KeyCode == Keys.A)
            {
                foreach (ListViewItem li in lvwDir.Items)
                    li.Selected = true;
            }
        }

        private void btn_Cut_Click(object sender, EventArgs e)
        {
            typeClipBorad = 2;
            clipBoard();
        }

        private void btn_conTextCut_Click(object sender, EventArgs e)
        {
            btn_Cut.RaiseClick();
        }

        private void btn_conTextRename_Click(object sender, EventArgs e)
        {
            btn_Rename.RaiseClick();
        }

        private void btn_conTextProperties_Click(object sender, EventArgs e)
        {
            btn_Properties.RaiseClick();
        }

        private void btn_conTextCopy_Click(object sender, EventArgs e)
        {
            btn_Copy.RaiseClick();
        }

        private void btn_conTextPaste_Click(object sender, EventArgs e)
        {
            btn_Paste.RaiseClick();
        }
        public void EmptyFolder(DirectoryInfo directoryInfo)
        {
            try
            {
                foreach (FileInfo file in directoryInfo.GetFiles())
                {

                    file.Delete();
                }
                foreach (DirectoryInfo subfolder in directoryInfo.GetDirectories())
                {
                    EmptyFolder(subfolder);
                }
            }
            catch { }
        }// end empty folder
        public void DeleteFolder(string strFolderName) 
        { 
            DirectoryInfo ThuMucNguonDir = new DirectoryInfo(strFolderName); 
            if (Directory.Exists(strFolderName)) 
            { 
                try 
                { 
                    EmptyFolder(ThuMucNguonDir); 
                    Directory.Delete(strFolderName); 
                } 
                catch { } 
            } 
        }// ket thuc deleteFolder

        private void btn_Delete_Click(object sender, EventArgs e)
        { 
            string text = txt_address.Text + "\\" + lvwDir.SelectedItems[0].Text;
            DeleteFolder(text);

        }

       
    }//end class form1
}
